package com.igeek.client;

import com.igeek.bean.Brand;
import com.igeek.bean.Car;
import com.igeek.bean.Cartype;
import com.igeek.bean.User;
import com.igeek.dao.BrandDao;
import com.igeek.dao.CarDao;

import com.igeek.dao.UserDaoImpl;
import com.igeek.db.DataDB;
import com.igeek.util.SystemEnum;
import com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI;

import java.nio.file.LinkPermission;
import java.time.LocalDate;
import java.util.*;

/**
 * 主系统，与用户做交互
 */
public class MainSys {

    private UserDaoImpl userDao = new UserDaoImpl();
    private CarDao carDao = new CarDao();
    BrandDao brand = new BrandDao();

    Scanner scanner = new Scanner(System.in);
    List<Car> cars = DataDB.getInstance().cars;
    List<Brand> brands = DataDB.getInstance().brands;
    public static void main(String[] args) {

        MainSys mainSys = new MainSys();
        cai(mainSys);
    }

    public static void cai(MainSys mainSys){
        System.out.println("" +
                "1.登陆\n" +
                "2.注册：用户名不能重复\n" +
                "3.最新二手车信息\n" +
                "4.搜索车辆\n" +
                "5.对比车辆\n" +
                "6.我的收藏\n" +
                "7.后台管理\n" +
                "8.退出系统");

        Scanner scanner = new Scanner(System.in);
        String input = scanner.next();

        switch (input){
            case "1":
                mainSys.login();
                break;
            case "2":
                mainSys.register();
                break;
            case "3":
                mainSys.showNewInfo(null);
                break;
            case "4":
                mainSys.search();
                break;
            case "5":
                mainSys.compare();
                break;
            case "6":
                mainSys.showmycollect();
                break;
            case "7" :
                mainSys.backManage();


        }
    }



    public void login(){

        System.out.println("请输入用户名：");
        String name = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();

        //从dao层获取到用户登录的返回结果

        int code = userDao.login(name,password);

        printMsg(code);

        if(code == 1){
            System.out.println("输入错误请重新输入");
            login();
        }
        MainSys mainSys = new MainSys();
        cai(mainSys);

    }

    public void register(){
        System.out.println("请输入用户名");
        String name = scanner.next();
        int code = userDao.checkname(name);

        printMsg(code);

        if(code == 3){
            System.out.println("用户名已存在请重新输出入");
            register();
        }else{
            System.out.println("请输入密码");
            String password = scanner.next();
            User user = new User(name,password);
            DataDB.getInstance().users.add(user);
            MainSys mainSys = new MainSys();
            cai(mainSys);

        }

    }

    public void showNewInfo(ArrayList<Car> carArrayList){
        if(carArrayList == null){
            carDao.showinfo();
        }
        System.out.println("1.收藏（必须登陆后才能进行收藏）\n" +
                "2.加入对比\n" +
                "3.购买（只能在登陆后才能看到该菜单项）\n" +
                "4.返回主菜单");

        int choose = scanner.nextInt();
        User user = DataDB.getInstance().currentUser;
        switch (choose){
            case 1:
                if(user == null){
                    System.out.println("请先登录");

                    login();
                }else{
                    System.out.println("输入收藏车辆序号");
                    int i = scanner.nextInt();
                    if(carArrayList == null){
                        boolean flag = true;
                        for(int j = 0;j < user.getCars().size();j ++) {
                            if(DataDB.cars.get(i - 1) == user.getCars().get(j)){
                                flag = false;
                            }
                        }
                        if(!flag){
                            System.out.println("已经存在");
                        }else{
                            user.setCars(DataDB.cars.get(i - 1));
                        }

                        showNewInfo(carArrayList);
                    }else {
                        boolean flag = true;
                        for(int j = 0;j < user.getCars().size();j ++) {
                            if(DataDB.cars.get(i - 1) == user.getCars().get(j)){
                                flag = false;
                            }
                        }
                        if(!flag){
                            System.out.println("已经存在");
                        }else{
                            user.setCars(carArrayList.get(i - 1));
                        }

                        showNewInfo(carArrayList);
                    }
                }
                break;
            case 2:
                System.out.println("输入比较车辆序号");
                int i = scanner.nextInt();
                if(carArrayList ==null){
                    carDao.compare(DataDB.cars.get(i - 1));
                }else{
                    carDao.compare(carArrayList.get(i - 1));
                }
                break;
            case 3:
                if (user == null) {
                    System.out.println("请先登录");
                    MainSys mainSys = new MainSys();
                    mainSys.login();

                }else{
                    System.out.println("输入购买车辆序号");
                     i = scanner.nextInt();

                     if(carArrayList ==null){
                         carDao.buy(DataDB.cars.get(i - 1));
                     }else{
                         carDao.buy(carArrayList.get(i - 1) );
                    }

                }
                break;
            case 4:
                MainSys mainSys = new MainSys();
                cai(mainSys);
                break;


        }

    }

    public void search(){
        System.out.println("1.根据品牌搜索\n" +
                "2.根据价格搜索\n" +
                "3.根据上牌日期搜索");
        String choose = scanner.next();
        if(choose.equals("1")){

            List<Car> cars =carDao.searchByBrand();

            System.out.println("输入数字选择查看品牌");
            int b = scanner.nextInt() - 1;


            Set<String> allcartypes= carDao.findallcartype(cars.get(b).getCartypeid());
            int index1 = 0;
            for(String cartype : allcartypes){
                System.out.print(++index1 + ". ");
                System.out.println(cartype);

            }

            System.out.println("选择车型");
            int choosetype = scanner.nextInt() ;
            int index2 =  0;
            String cartypename = null;
            for(String cartype : allcartypes){
                if(++index2 == choosetype){
                    cartypename = cartype;
                    break;
                }
            }
            ArrayList<Car> carArrayList = carDao.findallcar(cartypename);
            if(carArrayList.size() == 0){
                System.out.println("无此种车辆");
                search();
            }else{
                for(int i = 0;i < carArrayList.size();i ++){
                    int j  = i + 1;
                    System.out.print(j+". ");
                    System.out.println(carArrayList.get(i));
                }
                showNewInfo(carArrayList);
            }


        }else if(choose.equals("2")){
            System.out.println("1. 5万以下\n" +
                    "2. 5-10万\n" +
                    "3. 10-15万\n" +
                    "4. 15万以上");
            System.out.println("请选择查看多少钱的车");
            int i = scanner.nextInt();
            ArrayList<Car> allcar =  carDao.searchByPrice(i);

            if(allcar.size() == 0){
                System.out.println("无此种车辆");
            }else{
                for(int j = 0;j < allcar.size();j ++){
                    System.out.print(j+1 +". ");
                    System.out.print(carDao.findCarBrand(allcar.get(j).getCarid()));
                    System.out.println(carDao.findCartype(allcar.get(j).getCartypeid()));
                }

                System.out.println("选择查看车辆");
                i = scanner.nextInt();
                System.out.println(allcar.get(i - 1));
            }

            showNewInfo(allcar);

        }else if(choose.equals("3")){
            System.out.println("请输入起始年份");
            int y = scanner.nextInt();
            System.out.println("请输入起始月份");
            int m = scanner.nextInt();
            System.out.println("请输入日期");
            int d = scanner.nextInt();

            System.out.println("输入结束日期");
            int y1 = scanner.nextInt();
            System.out.println("输入结束月份");
            int m1 = scanner.nextInt();

            System.out.println("输入结束日期");
            int d1 = scanner.nextInt();

            LocalDate date1 = LocalDate.of(y,m,d);
            LocalDate date2 = LocalDate.of(y1,m1,d1);

            ArrayList<Car> carArrayList = carDao.searchByTime(date1,date2);
            for(int i = 0;i <carArrayList.size();i ++){
                System.out.print(i +". ");
                System.out.println(carArrayList.get(i));
            }

            showNewInfo(carArrayList);

        }


    }

    public void compare(){
        System.out.println("1.查看对比信息\n" +
                "2.删除对比车辆\n" +
                "3.返回上一级菜单");
        int choose = scanner.nextInt();
        switch (choose){
            case 1 :
                userDao.showcompareinfo(); break;
            case 2 :
                userDao.showcompareinfo();
                System.out.println("请选择删除的序号");
                int i = scanner.nextInt();
                userDao.deletecompare(i);
                System.out.println("删除完成");
                break;
            case 3:
                cai(new MainSys());
                break;

        }

    }

    public void showmycollect(){
        User user = DataDB.currentUser;
        if (user != null) {
            ArrayList<Car> cars = user.getCars();
            for(int i = 0;i < cars.size();i ++){
                System.out.print( i+1 +". ");
                System.out.println(cars.get(i));
            }
            showNewInfo(cars);
        }else{
            System.out.println("当前无用户请登录");
            login();
        }


    }

    public void backManage(){
        MainSys mainSys = new MainSys();
        if(DataDB.getInstance().currentUser == null){
            System.out.println("无当前用户请登录");
            mainSys.login();
        }

        if(DataDB.getInstance().currentUser.getPower() != 1){
            System.out.println("无此权限");

            cai(mainSys);
        }else{
            System.out.println("1.品牌管理\n" +
                    "2.车型管理\n" +
                    "3.发布车辆信息\n");

            int choose = scanner.nextInt();
            System.out.println("输入选择");
            MainSys mainSys1 = new MainSys();
            switch (choose){
                case 1 :
                    brand.insertBrand();
                    cai(mainSys);
                    break;
                case 2 :
                    carDao.cartypemanage();
                    cai(mainSys);
                    break;
                case 3:
                    List<Cartype> cartypes = DataDB.getInstance().cartypes;
                    for(int i = 0;i < brands.size();i ++){
                        System.out.println(i+1 +". ");
                        System.out.println(brands.get(i).getBname());
                    }
                    System.out.println("请选择品牌");
                    int i = scanner.nextInt();
                    int brand = brands.get(i).getBrandid();
                    BrandDao brandDao = new BrandDao();
                    ArrayList<Integer> cars1 = new ArrayList<>();
                    for(Car car : cars){
                        if(car.getBrandid() == brands.get(i).getBrandid()){
                            int id = car.getCartypeid();
                            for(Cartype cartype : cartypes){
                                if(cartype.getCartypeid() == id){
                                    System.out.println(cartype.getCartypename());
                                    cars1.add(cartype.getCartypeid());

                                }
                            }
                        }
                    }
                    System.out.println("输入选择的车型");
                    int j = scanner.nextInt();
                    int cartype = cars1.get(j - 1);
                    Car car = new Car(DataDB.getInstance().cars.size() + 1,brand,cartype);
                    car.setTime(LocalDate.now());
                    System.out.println("输入价格");
                    double price = scanner.nextDouble();
                    car.setPrice(price);
                    System.out.println("输入里程数");
                    int l = scanner.nextInt();
                    car.setDistance(l);
                    DataDB.getInstance().cars.add(car);
                    System.out.println("设置成功");

                    cai(mainSys1);
                    break;
            }
        }





    }

    public void printMsg(int code){
        if(code == SystemEnum.SUCCESS.getCode()){
            System.out.println(SystemEnum.SUCCESS.getMsg());
        }else if(code  == SystemEnum.ERROR_INPUT.getCode()){
            System.out.println(SystemEnum.ERROR_INPUT.getMsg());
        }else if(code == SystemEnum.ERROR_BALANCE.getCode()){
            System.out.println(SystemEnum.ERROR_BALANCE.getMsg());
        }else{
            System.out.println(SystemEnum.ERROR_REPEAT.getMsg());
        }
    }

}
